package com.example.jdbcdemo.config;

import com.example.jdbcdemo.service.TokenInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//实现登录拦截器的功能
@Component
public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Autowired
    TokenInterface tokenInterface;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //登录成功后，应该有用户的session
        String token= (String) request.getHeader("token");
        //如果没取到token或token过期就返回false
        if (token==null||!tokenInterface.verifyToken(token)){
            return false;
        }
        else {
            return true;
        }
    }


}
